package com.dbq.fast.business.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.invite.InviteActivity;
import com.dbq.fast.business.me.adapter.RewardAdapter;
import com.dbq.fast.business.me.api.RewardApi;
import com.dbq.fast.business.me.bean.RewardInfo;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RewardActivity extends BaseActivity {

    public static final int PAGE_SIZE = 10;

    @BindView(R.id.share)
    TextView share;
    @BindView(R.id.no_reward)
    TextView noReward;
    @BindView(R.id.reward_list)
    RecyclerView rewardList;
    @BindView(R.id.pbLoad)
    ProgressBar pbLoad;

    RewardAdapter adapter;

    private int nextPage = 0;
    private int totalPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reward);
        ButterKnife.bind(this);

        pbLoad.setVisibility(View.VISIBLE);
        initRecycleView();
        initLoadMoreListener();

        loadData(1);

    }

    private void initLoadMoreListener() {
        rewardList.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (nextPage > totalPage) return;

                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    adapter.changeMoreStatus(RewardAdapter.LOADING_MORE);

                    loadData(nextPage);

                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void loadData(int page) {
        RewardApi.queryMyReward(page, new RemoteApiCallBack<RewardInfo>() {
            @Override
            public void onFail(int status, String message) {
                pbLoad.setVisibility(View.GONE);
                Toast.makeText(App.getContext(), getString(R.string.tip_retry_network_bad), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(RewardInfo rewardInfos) {
                if (!isResume) return;

                pbLoad.setVisibility(View.GONE);
                totalPage = rewardInfos.getTotalPage();
                List<RewardInfo.AwardListBean> rewardsBeans = rewardInfos.getAwardList();
                if (rewardsBeans == null || rewardsBeans.size() == 0) {
                    noReward.setVisibility(View.VISIBLE);
                    rewardList.setVisibility(View.GONE);
                    if (adapter != null) {
                        adapter.changeMoreStatus(RewardAdapter.NO_LOAD_MORE);
                    }
                } else {
                    noReward.setVisibility(View.GONE);
                    rewardList.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        loadData(rewardsBeans);
                    } else {
                        moreData(rewardsBeans);
                    }

                    nextPage = rewardInfos.getCurPage() + 1;
                }
            }
        });
    }

    private void loadData(List<RewardInfo.AwardListBean> rewardInfos) {
        if (adapter != null) {
            adapter.setData(rewardInfos);
            adapter.changeMoreStatus(RewardAdapter.NO_LOAD_MORE);
        }
    }

    private void moreData(List<RewardInfo.AwardListBean> rewardInfos) {
        if (adapter != null) {
            adapter.addFooterData(rewardInfos);
            if (rewardInfos.size() == PAGE_SIZE) {
                adapter.changeMoreStatus(RewardAdapter.PULLUP_LOAD_MORE);
            } else {
                adapter.changeMoreStatus(RewardAdapter.NO_LOAD_MORE);
            }
        }
    }

    private void initRecycleView() {
        rewardList.setLayoutManager(new LinearLayoutManager(this));
        rewardList.setHasFixedSize(true);

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_line));
        rewardList.addItemDecoration(divider);

        adapter = new RewardAdapter();
        rewardList.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            case R.id.share:
                YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_CLICK_MY_REWARD_SHARE);
                startActivity(new Intent(this, InviteActivity.class));
                break;
        }
    }


}
